package com.mcptt.http.operator.online;



import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.OnlineInfo;

public class OnlineParser 
{
	private static final String SUB_KEY1 = "sip:";
	private static final String SUB_KEY2 = ";ob|";
	private static final String SUB_KEY3 = ";transport=UDP|";
	private static final String SUB_KEY4 = ";rinstance";
	public OnlineParser()
	{}
	public static List<OnlineInfo> parseContentStr(String bodyStr) {
		Log.debug(OnlineParser.class, "parseContentStr bodyStr = " + bodyStr);
		List<OnlineInfo> userList = new Vector<OnlineInfo>();

		String[] strArr = bodyStr.split("\n");
		for(int i = 0; i < strArr.length; i++)
		{
			Log.debug(OnlineParser.class, "parseContentStr strArr[" + i + "] = " + strArr[i]);
			String str = strArr[i];
			Log.debug(OnlineParser.class, "parseContentStr str = " + str);
			if(str != null
					&& str.length() > 0)
			{
				String[] attr = str.split("@");
				if(attr != null
						&& attr.length == 2)
				{
					String subStr = attr[0];
					int index = subStr.indexOf(SUB_KEY1);
					String mcId = subStr.substring(index+SUB_KEY1.length());

					subStr = attr[1];
					index = subStr.indexOf(SUB_KEY2);
					Log.debug(OnlineParser.class, "parseContentStr 1 index = " + index);
					if(index == -1) {
						index = subStr.indexOf(SUB_KEY3);
					}
					if(index == -1) {
						index = subStr.indexOf(SUB_KEY4);
					}
					Log.debug(OnlineParser.class, "parseContentStr 2 index = " + index);
					String ip =  subStr.substring(0,index);

					String[] ipAttr = ip.split(":");
					if(ipAttr != null
							&& ipAttr.length == 2)
					{
						ip = ipAttr[0];
					}
					Log.debug(OnlineParser.class, "parseContentStr mcId = " + mcId + " ip = " + ip);


					OnlineInfo info = new OnlineInfo(mcId,1,ip);
//					Date d = new Date();
//					long time = d.getTime();
//					LocationData locData = LocationData.getDefaultLocationData();
//					MapPointInfo point = new MapPointInfo(mcId,MapPointInfo.MapPointType_GPS,time,locData,"",ip);
//					Log.debug(OnlineParser.class, "parseContentStr point = " + point);

					if(!hasSameOne(userList,mcId))
					{
						userList.add(info);
					}
				}
			}
		}
		return userList;
	}
	private static boolean hasSameOne(List<OnlineInfo> userList,String mcId){
		for(Iterator<OnlineInfo> itor = userList.iterator();itor.hasNext();)
		{
			OnlineInfo info = itor.next();
			if(info.isSameOne(mcId))
			{
				return  true;
			}
		}
		return false;
	}
//	private static boolean hasSameOne(List<MapPointInfo> userList,String mcId){
//		for(Iterator<MapPointInfo> itor = userList.iterator();itor.hasNext();)
//		{
//			MapPointInfo info = itor.next();
//			if(info.isSameOne(mcId))
//			{
//				return  true;
//			}
//		}
//		return false;
//	}
}
